home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10780 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: news.axess.com!news
  2. From: tdrymona@axess.com (Kamikaze)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Pointer to non-static function
  5. Date: Sun, 10 Mar 1996 17:20:23 GMT
  6. Organization: Axess Communications, Montreal, Canada
  7. Message-ID: <4huok9$t8s@news.axess.com>
  8. References: <4hnsfs$cp2@nntp.ucs.ubc.ca>
  9. Reply-To: tdrymona@axess.com
  10. NNTP-Posting-Host: cretien.axess.com
  11. X-Newsreader: Forte Free Agent v0.55
  12.  
  13. jamesdf@unixg.ubc.ca (James Fairweather) wrote:
  14.  
  15. >I am attempting to make some code I've written more elegant by using a
  16. >pointer to a function.  Here's how I declare the pointer:
  17.  
  18. >    double (*f)(double);
  19.  
  20. >Now I'd like to make f point to one of two functions, one declared as:
  21.  
  22. >    double CFunction::Calculate(double);
  23.  
  24. >and the other as:
  25.  
  26. >    double CEquation::Calculate(double);
  27.  
  28. >Neither are static, and CEquation and CFunction are not related by
  29. >inheritance.  Nor can they be, since CEquation contains a list of
  30. >CFunctions.
  31.  
  32. >At compile time, the compiler issues an error, "cannot convert from
  33. >double (CFunction::*)(double) to double (__cdecl *)(double)" and
  34. >"cannot convert from double (CEquation::*)(double) to double (__cdecl
  35. >*)(double)".
  36.  
  37. >This looks suspiciously like a typecasting problem to me.  I'm
  38. >wondering if what I'm attempting to do is possible, and if so, how to
  39. >do it.  What is the correct way to do the typecast, if that is the
  40. >problem?  Any help is much appreciated.
  41.  
  42. >    James
  43.  
  44. The compiler inserts a this pointer as the first argument of every
  45. method in a class. You have to manually put in the this pointer into
  46. the method declaration.
  47.  
  48. So try
  49.     double (*f)(CFunction*, double);
  50.  
  51.  
  52.  
  53.  
  54.